#! /usr/bin/env python
# -*- coding: utf-8 -*-


import pkgutil
import sys
import subprocess
import shutil
import tarfile

def run_command(params, working_dir):
  ret = 1
  out = []
  try:
    p = subprocess.Popen(params,  stdout=subprocess.PIPE, stderr=subprocess.PIPE, cwd=working_dir)
    out = p.stdout.readlines()
  except OSError, e:
    ret = 0
    print 'Error %s' % e
  print out
  return ret

def install_package(package_name):
  return run_command(['zypper', '--quiet', '--non-interactive', 'install', '-f', package_name], '.')


for importer, modname, ispkg in pkgutil.iter_modules():
  if modname == "psycopg2":
     sys.exit(0)

ret = install_package('postgresql-devel') and install_package('python-devel')
if ret != 0:
  shutil.copy('/c4shares/Public/c4dev/yarova1/psycopg2/psycopg2-2.4.6.tar.gz', '/tmp/')
  tfile = tarfile.open("/tmp/psycopg2-2.4.6.tar.gz", 'r:gz')
  tfile.extractall('/tmp/')
  tfile.close()
  ret = run_command(['python', '/tmp/psycopg2-2.4.6/setup.py', 'build'], '/tmp/psycopg2-2.4.6') and run_command(['python', '/tmp/psycopg2-2.4.6/setup.py', 'install'], '/tmp/psycopg2-2.4.6')

